package com.slz3.business.module.systemManagement.sysUser.steward;

import com.google.gson.Gson;
import com.slz3.business.frame.module.DomainEntity;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.module.systemManagement.sysUser.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
public final class SysUserConverter {

    public static final String LOGIN_TOKEN = "loginToken";

    /**
     * 保存登录用户信息到redis
     */
    public static void saveLoginUser2Redis(SysUser sysUser){
        if (sysUser == null) return;
        Gson gson = new Gson();
        String loginToken = UUIDUtils.generateUUID().toString();
        sysUser.setLoginToken(loginToken);
        RedisClient.redisTemplate.opsForValue()
                .set(String.format("%s:%s", LOGIN_TOKEN, loginToken), gson.toJson(sysUser), 1800, TimeUnit.SECONDS);
    }

    /**
     * 从redis获取登录用户
     */
    public static SysUser loadLoginUserFromRedis(String loginToken){
        Object o = RedisClient.redisTemplate.opsForValue().get(String.format("%s:%s", LOGIN_TOKEN, loginToken));
        if (o == null){
            return null;
        }
        try {
            Gson gson = new Gson();
            SysUser sysUser = gson.fromJson(o.toString(), SysUser.class);
            RedisClient.redisTemplate.expire(String.format("%s:%s", LOGIN_TOKEN, loginToken), 1800, TimeUnit.SECONDS);
            return sysUser;
        }catch (Exception e){
            log.error("异常信息：", e);
            return null;
        }
    }

    public static SysUser loadLoginUserFromRequest(ServerRequest request){
        List<String> loginTokenList = request.headers().header("authorization");
        if (CollectionUtils.isEmpty(loginTokenList)) {
            return null;
        }
        String loginToken = loginTokenList.get(0);
        return SysUserConverter.loadLoginUserFromRedis(loginToken);
    }

    public static UUID getLoginUserUuidFromRequest(ServerRequest request){
        SysUser sysUser = loadLoginUserFromRequest(request);
        return sysUser == null ? null : sysUser.getUuid();
    }

    public static void setSaveUpdateUser(ServerRequest req, DomainEntity domainEntity, boolean isAdd) {
        if (domainEntity == null){
            return;
        }
        UUID loginUserUuid = getLoginUserUuidFromRequest(req);
        if (isAdd){
            domainEntity.setCreator(loginUserUuid);
        }else {
            domainEntity.setLastModifier(loginUserUuid);
        }
    }

    public static void logout(ServerRequest req) {
        List<String> loginTokenList = req.headers().header("authorization");
        if (CollectionUtils.isEmpty(loginTokenList)) {
            return;
        }
        String loginToken = loginTokenList.get(0);
        RedisClient.redisTemplate.delete(String.format("%s:%s", LOGIN_TOKEN, loginToken));
    }
}
